package tcp;

import java.util.Arrays;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.ip.IpParameters;
import com.serotonin.modbus4j.msg.ReportSlaveIdRequest;
import com.serotonin.modbus4j.msg.ReportSlaveIdResponse;

public class treportSlaveld {
	public static String ipAdress;
	public static int ipPort;
	public static int slaveId;
	public treportSlaveld(String ipAdress1, int ipPort1, int slaveId1)
	{
		ipAdress = ipAdress1; 
		ipPort = ipPort1;
		 slaveId = slaveId1;
	};
	public static void remain1(){
		IpParameters ipParameters = new IpParameters();
        ipParameters.setHost(ipAdress);
        ipParameters.setPort(ipPort);
		ModbusFactory modbusFactory = new ModbusFactory();

		ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        reportSlaveIdTest(master, slaveId);
	};
	
	 public static void reportSlaveIdTest(ModbusMaster master, int slaveId3) {
	        try {
	            ReportSlaveIdRequest request = new ReportSlaveIdRequest(slaveId3);
	            ReportSlaveIdResponse response = (ReportSlaveIdResponse) master.send(request);

	            if (response.isException())
	                System.out.println("Exception response: message=" + response.getExceptionMessage());
	            else
	                System.out.println(Arrays.toString(response.getData()));
	        }
	        catch (ModbusTransportException e) {
	            e.printStackTrace();
	        }
	    }
}
